﻿using Core.DataAccess.Model.Project.Comment;
using Core.DataAccess.Model.Project.Information;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IComment;
using Core.IBusiness.IInformation.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Core.Web.Controllers.Information
{
    /// <summary>
    /// 信息评论管理
    /// </summary>
    [Route(".v1/Comment/[controller]")]
    [ApiController]
    public class CommentController : BaseApiController
    {
        public CommentController(ICommentModule iComment) :
            base(iComment: iComment)
        {

        }
        

        #region Serach

        /// <summary>
        /// 查询指定文章评论
        /// </summary>
        /// <param name="model">查询实体</param>
        /// <returns></returns>
        [HttpPost("getcomment")]
        [IgnoreAuthorize]
        public IResult GetComments(CommentRequest model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.GetComments(
                        model.infoKey,
                        model.isGetReply,
                        model.pagination,
                        model.attributes,
                        model.type), model.pagination);
                },
                model.IsValid(IsValidEnum.select));
        }


        #endregion







        #region Extend Attrs


        /// <summary>
        /// 点赞评论信息
        /// </summary>
        /// <param name="commentKey">评论关键字</param>
        /// <returns></returns>
        [HttpPost(".star/{commentKey}")]
        public IResult CommentStar(int commentKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentStar(
                        commentKey,
                        iClientInfo.Key,
                        iClientInfo.Token));
                },
                new Tuple<bool, string>(commentKey > 0, commentKey > 0 ? "" : "params: commentKey is null"));
        }


        /// <summary>
        /// 取消点赞评论信息
        /// </summary>
        /// <param name="commentKey">评论信息</param>
        /// <returns></returns>
        [HttpPost(".unstar/{commentKey}")]
        public IResult CommentUnStar(int commentKey)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentUnStar(
                        commentKey,
                        iClientInfo.Key,
                        iClientInfo.Token));
                },
                new Tuple<bool, string>(commentKey > 0, commentKey > 0 ? "" : "params: infoKey is null"));
        }


        #endregion







        #region Comment exec

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IResult Post(CommentList model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentExec(
                        model,
                        SqlExecType.insert,
                        iClientInfo));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IResult Put(CommentList model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentExec(
                        model,
                        SqlExecType.update,
                        iClientInfo));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentExec(
                        new CommentList
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        },
                        SqlExecType.delete,
                        iClientInfo));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }



        /// <summary>
        /// 添加评论属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(".attr")]
        public IResult PostAttr(CommentAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentAttributeExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 修改评论属性
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(".attr")]
        public IResult PutAttr(CommentAttribute model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentAttributeExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }

        /// <summary>
        /// 删除评论属性
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete(".attr{id}")]
        public IResult DeleteAttr(int id)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iComment.CommentAttributeExec(
                        new CommentAttribute
                        {
                            Id = id
                        },
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }



        #endregion


    }
}
